class Pickup < ActiveRecord::Base
  has_one :pickup_password
  validates_presence_of :avid, :message => '请填写推荐投稿'
  validates_presence_of :reason, :message => '请填写推荐理由'
  DEFAULT_STATUS = {0 => '未审核', 1 => '审核通过', 2 => '启用', 3 => '忽略'}
  DEFAULT_TYPE = {0 => '正常', 1 => '高人气', 2 => '自推荐', 3 => '超长理由', 4 => '新投稿推荐', 5 => '代提交'}
  def work
    Work.find_by_wid avid
  end

  def password
    pickup_password.password if pickup_password
  end

  def dai_uname
    PickupLog.find_by_pickup_id(id, :conditions => "otype = 3").uname rescue nil
  end
  
  def update_name
    update_attribute :wname, work.name
  end

  def check_type w=nil
    begin
      return 3 if reason.size > 120
      w ||= work
      return 4 if (Time.now-work.cdate) < 1296000
      return 2 if w.author_name == uname
      return 1 if WeeklyResult.first :include => :weekly_result_work, :conditions => ["weekly_result_works.wid = ? and score_rank < 100", avid]
      0
    rescue
      0
    end
  end
end
